package org.soc.timesheet.domain;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@Table(name = "CUSTOMERS_ENABLED")
@Configurable
@RooJavaBean
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "userss" })
@RooJson
@RooJpaActiveRecord(versionField = "", table = "CUSTOMERS_ENABLED", finders = { "findCustomersEnabledsByCustomerKeyEquals" })
@JsonIgnoreProperties({ "userss" })
public class CustomersEnabled {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @OneToMany(mappedBy = "idCustomer")
    private Set<Users> userss;

    @Column(name = "CUSTOMER_KEY", length = 100)
    private String customerKey;

    @Column(name = "CUSTOMER_DESC", length = 45)
    private String customerDesc;

    public Set<Users> getUserss() {
        return userss;
    }

    public void setUserss(Set<Users> userss) {
        this.userss = userss;
    }

    public String getCustomerKey() {
        return customerKey;
    }

    public void setCustomerKey(String customerKey) {
        this.customerKey = customerKey;
    }

    public String getCustomerDesc() {
        return customerDesc;
    }

    public void setCustomerDesc(String customerDesc) {
        this.customerDesc = customerDesc;
    }

    public static Long countFindCustomersEnabledsByCustomerKeyEquals(String customerKey) {
        if (customerKey == null || customerKey.length() == 0) throw new IllegalArgumentException("The customerKey argument is required");
        EntityManager em = CustomersEnabled.entityManager();
        TypedQuery q = em.createQuery("SELECT COUNT(o) FROM CustomersEnabled AS o WHERE o.customerKey = :customerKey", Long.class);
        q.setParameter("customerKey", customerKey);
        return ((Long) q.getSingleResult());
    }

    public static TypedQuery<CustomersEnabled> findCustomersEnabledsByCustomerKeyEquals(String customerKey) {
        if (customerKey == null || customerKey.length() == 0) throw new IllegalArgumentException("The customerKey argument is required");
        EntityManager em = CustomersEnabled.entityManager();
        TypedQuery<CustomersEnabled> q = em.createQuery("SELECT o FROM CustomersEnabled AS o WHERE o.customerKey = :customerKey", CustomersEnabled.class);
        q.setParameter("customerKey", customerKey);
        return q;
    }

    public static TypedQuery<CustomersEnabled> findCustomersEnabledsByCustomerKeyEquals(String customerKey, String sortFieldName, String sortOrder) {
        if (customerKey == null || customerKey.length() == 0) throw new IllegalArgumentException("The customerKey argument is required");
        EntityManager em = CustomersEnabled.entityManager();
        StringBuilder queryBuilder = new StringBuilder("SELECT o FROM CustomersEnabled AS o WHERE o.customerKey = :customerKey");
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            queryBuilder.append(" ORDER BY ").append(sortFieldName);
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                queryBuilder.append(" ").append(sortOrder);
            }
        }
        TypedQuery<CustomersEnabled> q = em.createQuery(queryBuilder.toString(), CustomersEnabled.class);
        q.setParameter("customerKey", customerKey);
        return q;
    }

    @PersistenceContext
    transient EntityManager entityManager;

    public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

    public static final EntityManager entityManager() {
        EntityManager em = new CustomersEnabled().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countCustomersEnableds() {
        return entityManager().createQuery("SELECT COUNT(o) FROM CustomersEnabled o", Long.class).getSingleResult();
    }

    public static List<CustomersEnabled> findAllCustomersEnableds() {
        return entityManager().createQuery("SELECT o FROM CustomersEnabled o", CustomersEnabled.class).getResultList();
    }

    public static List<CustomersEnabled> findAllCustomersEnableds(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM CustomersEnabled o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, CustomersEnabled.class).getResultList();
    }

    public static CustomersEnabled findCustomersEnabled(Integer id) {
        if (id == null) return null;
        return entityManager().find(CustomersEnabled.class, id);
    }

    public static List<CustomersEnabled> findCustomersEnabledEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM CustomersEnabled o", CustomersEnabled.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static List<CustomersEnabled> findCustomersEnabledEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM CustomersEnabled o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, CustomersEnabled.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            CustomersEnabled attached = CustomersEnabled.findCustomersEnabled(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public CustomersEnabled merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        CustomersEnabled merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("userss").toString();
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public String toJson(String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(this);
    }

    public static CustomersEnabled fromJsonToCustomersEnabled(String json) {
        return new JSONDeserializer<CustomersEnabled>().use(null, CustomersEnabled.class).deserialize(json);
    }

    public static String toJsonArray(Collection<CustomersEnabled> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static String toJsonArray(Collection<CustomersEnabled> collection, String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(collection);
    }

    public static Collection<CustomersEnabled> fromJsonArrayToCustomersEnableds(String json) {
        return new JSONDeserializer<List<CustomersEnabled>>().use("values", CustomersEnabled.class).deserialize(json);
    }
}
